package com.living.filedownloader.ui.audiosel;

import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloadSampleListener;
import com.living.R;
import com.living.filedownloader.bean.TasksManagerModel;
import com.living.filedownloader.db.DownLoadDBManger;
import com.md.network.api.Audio;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;

/***
 * 下载适配器
 */
public class TaskItemSelectAdapter extends BaseQuickAdapter<Audio, BaseViewHolder> {

    public Map<String, Audio> mapSel;

    /**
     * 下载监听
     */
    private FileDownloadListener taskDownloadListener = new FileDownloadSampleListener() {
        @Override
        protected void completed(BaseDownloadTask task) {
            super.completed(task);
            if (task == null) {
                return;
            }
        }
    };

    public TaskItemSelectAdapter(Map<String, Audio> mapSel) {
        super(R.layout.item_downloaded_task);
        this.mapSel = mapSel;
    }

    @Override
    public void setNewInstance(@Nullable List<Audio> list) {
        for (Audio audio : list) {
            audio.setDownloadStatus(DownLoadDBManger.getInstance().isAudioExite(audio.getAlbumId(), audio.getId()) ? 1 : 0);
        }
        super.setNewInstance(list);
    }

    @NotNull
    @Override
    protected BaseViewHolder onCreateDefViewHolder(@NotNull ViewGroup parent, int viewType) {
        BaseViewHolder holder = super.onCreateDefViewHolder(parent, viewType);
        return holder;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, Audio model) {
        holder.setText(R.id.task_name_tv, model.getId() + "  " + model.getName());
        holder.setText(R.id.task_status_tv, model.getAuthor() + "");
        if (model.getDownloadStatus() == 1) {
            holder.setImageResource(R.id.task_action_select, R.drawable.icon_download_disable);
        } else {
            if (mapSel.containsKey(model.getId())) {
                holder.setImageResource(R.id.task_action_select, R.drawable.icon_download_selected);
            } else {
                holder.setImageResource(R.id.task_action_select, R.drawable.icon_download_select);
            }
        }
        holder.setGone(R.id.task_action_more, true);
    }


}
